package com.example.service.impl;

import com.example.mapper.EmployeeMapper;
import com.example.pojo.dto.EmployeeInsertDTO;
import com.example.pojo.dto.EmployeeLoginDTO;
import com.example.pojo.dto.EmployeePageDTO;
import com.example.pojo.dto.EmployeeUpdateDTO;
import com.example.pojo.entity.Employee;
import com.example.pojo.entity.result.PageResult;
import com.example.pojo.vo.EmployeePageVO;
import com.example.pojo.vo.EmployeeVO;
import com.example.service.EmployeeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import context.BaseContext;
import exception.AccountNotFoundException;
import exception.PasswordErrorException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

import static com.baomidou.mybatisplus.core.toolkit.Wrappers.lambdaQuery;

@Service
@Slf4j
@RequiredArgsConstructor
public class EmployeeServiceImpl implements EmployeeService {
    private final EmployeeMapper employeeMapper;

    /**
     * 新增职工
     *
     * @param employeeInsertDTO
     */
    @Override
    public void insertEmp(EmployeeInsertDTO employeeInsertDTO) {
        employeeInsertDTO.setCreateEmp(BaseContext.getCurrentId());

        //设置创建时间
        employeeInsertDTO.setCreateTime(LocalDateTime.now());

        //设置创建者
        Long currentId = BaseContext.getCurrentId();
        System.out.println(currentId);
        employeeInsertDTO.setCreateEmp(currentId);

        //密码加密（使用Base64加密）
        employeeInsertDTO.setPassword(Base64.getEncoder().encodeToString(employeeInsertDTO.getPassword().getBytes()));
        employeeMapper.insertEmp(employeeInsertDTO);
    }

    /**
     * 删除职工
     *
     * @param id
     */
    @Override
    public void deleteEmp(Long id) {
        employeeMapper.deleteEmp(id);
    }

    /**
     * 修改员工信息
     *
     * @param employeeUpdateDTO
     */
    @Override
    public void updateEmp(EmployeeUpdateDTO employeeUpdateDTO) {
        if (employeeUpdateDTO.getPassword() != null) {
            employeeUpdateDTO.setPassword(Base64.getEncoder().encodeToString(employeeUpdateDTO.getPassword().getBytes()));
        }
        employeeMapper.updateEmp(employeeUpdateDTO);
    }

    /**
     * 查询职工信息
     *
     * @param id
     * @return
     */
    @Override
    public EmployeeVO getEmpById(Long id) {
        Employee employee = employeeMapper.selectEmpById(id);
        return EmployeeVO.builder()
                .id(employee.getId())
                .account(employee.getAccount())
                .name(employee.getName())
                .gender(employee.getGender())
                .build();
    }

    /**
     * 分页查询职工信息
     *
     * @param employeePageDTO
     * @return
     */
    @Override
    public PageResult<EmployeePageVO> pageEmployee(EmployeePageDTO employeePageDTO) {
        //设置分页条件
        PageHelper.startPage(employeePageDTO.getPage(), employeePageDTO.getPageSize());

        //执行查询
        List<Employee> empList = employeeMapper.listByQuery(employeePageDTO);

        List<EmployeePageVO> empPageVOList = new ArrayList<>();
        for (Employee emp : empList) {
            EmployeePageVO employeePageVO = new EmployeePageVO();
            BeanUtils.copyProperties(emp, employeePageVO);
            empPageVOList.add(employeePageVO);
        }

        Page<Employee> page = (Page<Employee>) empList;

        //返回分页结果
        return new PageResult<>(page.getTotal(), empPageVOList);
    }

    /**
     * 登录
     *
     * @return
     */
    @Override
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String account = employeeLoginDTO.getAccount();
        //String password = employeeLoginDTO.getPassword();
        //md5加密后再比对
        String password = Base64.getEncoder().encodeToString(employeeLoginDTO.getPassword().getBytes());

        //1、根据用户名查询数据库中的数据
        Employee employee = employeeMapper.getByAccount(account);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException("账号不存在");
        }

        //密码比对
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException("密码错误");
        }
        //3、返回实体对象
        return employee;
    }


}
